package demoMod.invincibleOne.cards.invincible;

import basemod.BaseMod;
import com.megacrit.cardcrawl.actions.AbstractGameAction;
import com.megacrit.cardcrawl.actions.common.MakeTempCardInHandAction;
import com.megacrit.cardcrawl.cards.AbstractCard;
import com.megacrit.cardcrawl.characters.AbstractPlayer;
import com.megacrit.cardcrawl.core.CardCrawlGame;
import com.megacrit.cardcrawl.dungeons.AbstractDungeon;
import com.megacrit.cardcrawl.localization.CardStrings;
import com.megacrit.cardcrawl.monsters.AbstractMonster;
import demoMod.invincibleOne.InvincibleOneMod;
import demoMod.invincibleOne.actions.SelectCardInCardGroupAction;
import demoMod.invincibleOne.enums.CardTagsEnum;
import demoMod.invincibleOne.interfaces.IOnCurseAddToHand;

import javax.smartcardio.Card;
import java.util.List;
import java.util.stream.Collectors;

public class Transcribe extends AbstractInvincibleCard {
    public static final String ID = InvincibleOneMod.makeID("Transcribe");

    private static final CardStrings cardStrings = CardCrawlGame.languagePack.getCardStrings(ID);
    public static final String NAME = cardStrings.NAME;
    public static final String DESCRIPTION = cardStrings.DESCRIPTION;
    public static final String IMG_PATH = "cards/Transcribe.png";

    private static final CardType TYPE = CardType.SKILL;
    private static final CardRarity RARITY = CardRarity.UNCOMMON;
    private static final CardTarget TARGET = CardTarget.NONE;

    private static final int COST = -2;

    public Transcribe() {
        super(ID, NAME, InvincibleOneMod.getResourcePath(IMG_PATH), COST, DESCRIPTION, TYPE, RARITY, TARGET);
        this.tags.add(CardTagsEnum.VIRUS);
    }

    @Override
    public Runnable getUpgradeAction() {
        return () -> {
            this.rawDescription = cardStrings.UPGRADE_DESCRIPTION;
            this.initializeDescription();
        };
    }

    @Override
    public void use(AbstractPlayer p, AbstractMonster m) {
        if (upgraded) {
            addToBot(new SelectCardInCardGroupAction(1, c ->
                    (c.type == CardType.CURSE || c.hasTag(CardTagsEnum.VIRUS)) && !c.cardID.equals(ID), c -> {
                addToTop(new MakeTempCardInHandAction(c));
                if (AbstractDungeon.player.hand.size() < BaseMod.MAX_HAND_SIZE) {
                    if (c.hasTag(CardTagsEnum.VIRUS)) {
                        c.use(p, m);
                    }
                    IOnCurseAddToHand.curseAddToHand(c);
                }
            }, p.hand, SelectCardInCardGroupAction.TEXT[3], false));
        } else {
            addToBot(new AbstractGameAction() {
                @Override
                public void update() {
                    List<AbstractCard> cards = p.hand.group.stream().filter(c -> (c.type == CardType.CURSE || c.hasTag(CardTagsEnum.VIRUS)) && !c.cardID.equals(ID)).collect(Collectors.toList());
                    if (!cards.isEmpty()) {
                        AbstractCard card = cards.remove(AbstractDungeon.cardRandomRng.random(cards.size() - 1));
                        addToTop(new MakeTempCardInHandAction(card));
                        // todo: 其实这个手牌数量判断不是很严谨，AddToTop并不是立即发生，但好像不太重要。
                        if (card.hasTag(CardTagsEnum.VIRUS) && AbstractDungeon.player.hand.size() < BaseMod.MAX_HAND_SIZE) {
                            card.use(p, m);
                            IOnCurseAddToHand.curseAddToHand(card);
                        }
                    }
                    isDone = true;
                }
            });
        }
    }
}
